package com.cloud.pole.shop.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.cloud.pole.common.handler.GlobalExceptionHandler;
import com.cloud.pole.common.interceptor.AdminAuthRestInterceptor;
import com.cloud.pole.common.interceptor.MerchantAuthRestInterceptor;
import com.cloud.pole.common.interceptor.ServiceAuthRestInterceptor;
import com.cloud.pole.common.interceptor.UserAuthRestInterceptor;
import java.util.ArrayList;
import java.util.Collections;

@Configuration("admimWebConfig")
@Primary
public class WebConfiguration extends WebMvcConfigurerAdapter {
	@Bean
	GlobalExceptionHandler getGlobalExceptionHandler() {
		return new GlobalExceptionHandler();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 客户端调用需要过滤的(需要过滤token和client-token)
		/*网关上增加了参数client-token，防止从微服务端口直接访问*/
		registry.addInterceptor(getServiceAuthRestInterceptor())
				.addPathPatterns(getClientPathPatterns().toArray((new String[] {})))
				.addPathPatterns(getFrontPathPatterns().toArray(new String[] {}))
				.addPathPatterns(getAdminPathPatterns().toArray(new String[] {}))
				.addPathPatterns(getMerchantPathPatterns().toArray(new String[] {}));
		// 前端调用需要过滤的
		registry.addInterceptor(getUserAuthRestInterceptor()).addPathPatterns(getFrontPathPatterns().toArray(new String[] {}));
		// 后台调用需要过滤
		registry.addInterceptor(getAdminAuthRestInterceptor()).addPathPatterns(getAdminPathPatterns().toArray(new String[] {}));
		
		// 商家端口需要过滤
		registry.addInterceptor(getMerchantAuthRestInterceptor()).addPathPatterns(getMerchantPathPatterns().toArray(new String[] {}));
	}

	@Bean
	ServiceAuthRestInterceptor getServiceAuthRestInterceptor() {
		return new ServiceAuthRestInterceptor();
	}

	@Bean
	UserAuthRestInterceptor getUserAuthRestInterceptor() {
		return new UserAuthRestInterceptor();
	}

	@Bean
	AdminAuthRestInterceptor getAdminAuthRestInterceptor() {
		return new AdminAuthRestInterceptor();
	}
	
	@Bean
	MerchantAuthRestInterceptor getMerchantAuthRestInterceptor() {
		return new MerchantAuthRestInterceptor();
	}

	/**
	 * 需要用户和服务认证判断的路径（前端）
	 * 
	 * @return
	 */
	private ArrayList<String> getFrontPathPatterns() {
		ArrayList<String> list = new ArrayList<>();
		String[] urls = {"/goods/front/addCollection", 
				         "/goods/front/cancelCollection", 
				          "/shoppingCart/front/**", 
				          "/order/front/**",
				          "/orderPay/front/doOrderPay",
				          "/orderPay/front/getToOrderPay",
				          "/userDeliAddr/front/**",
				          "/orderComment/front/**",
				          "/goodsCollection/front/**",
				          "/orderRefund/front/**"};		
		Collections.addAll(list, urls);
		return list;
	}

	/**
	 * 需要用户和服务认证判断的路径（后台地址）
	 * 
	 * @return
	 */
	private ArrayList<String> getAdminPathPatterns() {
		ArrayList<String> list = new ArrayList<>();
		String[] urls = { 
						"/goods/goods/admin/**",
						"/order/order/admin/*",
						"/order/refund/admin/*",
						"/order/comment/admin/*",
						"/shops/admin/*",
						"/goods/promotion/admin/*"};		
		Collections.addAll(list, urls);
		return list;
	}
	
	/**
	 * 需要用户和服务认证判断的路径（商家管理地址）
	 * @return
	 */
	private ArrayList<String> getMerchantPathPatterns() {
		ArrayList<String> list = new ArrayList<>();
		String[] urls = { 
				"/goods/goods/admin_merch/**",
				"/goods/promotion/admin_merch/*",
				"/order/order/admin_merch/*",
				"/order/refund/admin_merch/*",
				"/order/comment/admin_merch/*",
				"/shops/admin_merch/*"
				};
		Collections.addAll(list, urls);
		return list;
	}

	/**
	 * 需要用户和服务认证判断的路径(客户端)
	 * @return
	 */
	private ArrayList<String> getClientPathPatterns() {
		ArrayList<String> list = new ArrayList<>();
		String[] urls = { "/api/shops/**" };
		Collections.addAll(list, urls);
		return list;
	}
}

